package com.example.androidtest;

import java.util.Timer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;


public class MainActivity extends Activity {
	MySurfaceView myView;
//	GameView myView;
	
	public static final int DATA_RECEIVED=0;

	Point point = new Point();
	Paint paint = new Paint();
	Path path = new Path();
	Timer timer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 
		setContentView(R.layout.activity_main);
		myView=(MySurfaceView)findViewById(R.id.mySurfaceView1);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏显示
		
//		myView=new GameView(this);
		
//		myView = new MySurfaceView(this);
		timer=new Timer();
		AddTask at=new AddTask(mHandler,this);
		timer.schedule(at, 1000, 42);		
//		setContentView(myView);// 使其显示视图
		
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	Handler mHandler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch (msg.what) {
			case DATA_RECEIVED:
				myView.onDataReceived();
				break;

			default:
				break;
			}
		}
	};
	
//	public boolean onTouchEvent(MotionEvent event) {
//        // TODO Auto-generated method stub
//		Log.d("MainActivity", "cordination: "+event.getX()+" "+event.getY());
//		
//        myView.OnTouch(event);
//        myView.postInvalidate();
//        return super.onTouchEvent(event);
//	}
	
	public class GameView extends SurfaceView implements SurfaceHolder.Callback {


        SurfaceHolder surfaceHolder;

        

        public GameView(Context context) {

                super(context);

                // TODO Auto-generated constructor stub

                surfaceHolder = this.getHolder();  

                surfaceHolder.addCallback(this);       

                this.setFocusable(true);  

        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        public void surfaceCreated(SurfaceHolder holder) {

        }

        public void surfaceDestroyed(SurfaceHolder holder) {

        }

        

        public void OnTouch(MotionEvent event){
   
                paint.setColor(Color.RED);
          paint.setStyle(Paint.Style.STROKE);   //关键在于设置这个   ，默认情况下为实心！！
    
          paint.setStrokeWidth(3);//设置线条粗细
            switch (event.getAction()) 
            {
             case MotionEvent.ACTION_DOWN:
             path.moveTo(event.getX(), event.getY()-40);//设置起点
              break;
              
             case MotionEvent.ACTION_MOVE:
             path.lineTo(event.getX(), event.getY()-40);      
             Canvas canvas = surfaceHolder.lockCanvas();
             canvas.drawPath(path, paint);//指定轨迹画图       由于path是多个轮廓叠加在一起，所以可以绘制在一张图上面。越往后，path中包含的轮廓越多！   
             surfaceHolder.unlockCanvasAndPost(canvas);
              break;
              
             case MotionEvent.ACTION_UP:
              break;
              
             default:            
              break;
            }//switch

           
        }//Ontouch

}  //GameView

}
